<template>
  <div id="system_role">
    <!--顶部搜索框内容开始-->
    <div class="role_seek">
      <el-input
        class="role_input"
        placeholder="请输入角色名称"
        v-model="forms.input"
        clearable
        @change="seek"
      ></el-input>
      <el-button type="primary" icon="el-icon-search" size="mini" v-power="'admin:role.list'"
        >查找</el-button
      >
      <el-button size="mini" type="primary" icon="el-icon-edit" @click="add" v-power="'admin:role.create'"
        >添加</el-button
      >
      <!--修改添加弹出框开始-->
      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form
          :model="form"
          :rules="rules"
          ref="form"
          class="pop-up"
          status-icon
        >
          <el-form-item
            prop="name"
            label="角色名称"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item
            prop="desc"
            label="说明"
            :label-width="formLabelWidth"
            class="pop-up"
          >
            <el-input v-model="form.desc" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelbutton('form')">取 消</el-button>
          <el-button
            type="primary"
            @click="bool ? adduser('form') : compile('form')"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <!--修改添加弹出框结束-->
    </div>
    <!--顶部搜索框内容结束-->

    <!--底部列表内容部分-->
    <template>
      <el-table
        border
        :data="tableData"
        style="width: 100%"
        :default-sort="{ prop: 'date', order: 'descending' }"
      >
        <el-table-column
          prop="name"
          label="角色名称"
          sortable
          align="center"
        ></el-table-column>
        <el-table-column
          prop="desc"
          label="说明"
          align="center"
        ></el-table-column>
        <el-table-column prop="address" label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              class="button_redact"
              size="mini"
              v-power="'admin:role.update'"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              class="button_remove"
              size="mini"
              type="danger"
               v-power="'admin:role.delete'"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
            <el-button
              class="button_redact"
              size="mini"
              type="danger"
               v-power="'admin:role.permissionList'"
              @click="popups(scope.$index, scope.row)"
              >授权</el-button
            >
            <!--授权弹出框开始-->
            <el-dialog
              :title="title"
              :visible.sync="AuthorizaPopup"
              class="title"
            >
              <div class="popup_trees">
                <el-tree
                  :data="list"
                  show-checkbox
                  node-key="id"
                  :default-checked-keys="assignedPermissions"
                  ref="doms"
                  ><!--第三层绿标-->
                  <span class="custom-tree-node" slot-scope="{ node, data }">
                    <span>{{ node.label }}</span>
                    <span
                      class="el-tag el-tag--success el-tag--mini el-tag--light"
                      v-if="data.api"
                      >{{ data.api }}</span
                    >
                  </span>
                </el-tree>
                <div slot="footer" class="dialog-footer">
                  <el-button @click="AuthorizaPopup = false">取 消</el-button>
                  <el-button type="primary" @click="Authoriza">确 定</el-button>
                </div>
              </div>
            </el-dialog>
            <!--授权弹出框结束-->
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!--底部列表内容部分结束-->

    <!--底部分页开始-->
    <div class="block">
      <Paging :forms="forms" :gain="gain"></Paging>
    </div>
    <!--底部分页结束-->
  </div>
</template>

<script>
import Paging from "../../components/Paging.vue"; //自定义组件
import Role from "@/api/system/Role";
export default {
  data() {
    //验证角色名称是否为空
    var username = (rule, value, callback) => {
      if (value === "") {
        //非空验证
        callback(new Error("角色名称不能为空"));
      } else {
        callback();
      }
    };
    //验证说明是否为空
    var explain = (rule, value, callback) => {
      if (value === "") {
        //非空验证
        callback(new Error("说明不能为空"));
      } else {
        callback();
      }
    };
    return {
      list: [],

      //弹出框
      tableData: [{}], //table里面的数据
      dialogFormVisible: false, //控制弹出框是否显示
      AuthorizaPopup: false,
      formLabelWidth: "120px", //弹出框宽度
      title: "创建", //弹出框标题
      bool: true, //布尔值 判断编辑还是修改 为true是创建 否则就是修改

      //分页
      forms: {
        input: "", //index框输入内容
        pageSize: 20, //limit
        total: 0, //分页——-共几条
        pageNo: 1 //页码
      },

      form: {
        name: "", //弹出框输入的角色名称
        desc: "", //弹出框输入的说明
        id: "" //角色的id
      },

      //弹出框验证
      rules: {
        name: [{ required: true, validator: username, trigger: "blur" }], //如果角色名称验证是正确的就给蓝色
        desc: [{ required: true, validator: explain, trigger: "blur" }] //如果说明验证是正确的就给蓝色
      },
      //点击当前的下标
      index: "", //当前点击的下标
      cancelIndex: 0,
      id: "", //点击当前的id

      //授权
      assignedPermissions: [] //接收dom数据
    };
  },
  components: {
    Paging
  },
  created() {
    this.gain();
  },
  methods: {
    //点击取消按钮
    cancelbutton() {
      this.dialogFormVisible = false;
    },
    //获取列表数据内容
    gain() {
      var _this = this;
      Role.gain(this.forms)
        .then(res => {
          _this.tableData = res.data.data.items; //将接口列表内容赋值给列表数组
          _this.forms.total = res.data.data.total; //分页————条数
          _this.forms.pageSize = res.data.data.pageSize; //页码长度
          _this.forms.pageNo = res.data.data.pageNo; //页码
        })
        .catch(err => {
          return err;
        });
    },
    //点击添加按钮触发
    add() {
      if (this.$refs.form) {
        this.$refs.form.resetFields();
      }
      this.title = "创建"; //将标题改变
      this.bool = true; //bool为true触发添加接口
      this.form.name = ""; //将角色名称清空
      this.form.desc = ""; //将说明清空
      this.dialogFormVisible = true; //显示弹出框
    },
    //添加新角色接口
    adduser(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          var _this = this;
          Role.adduser(_this.form)
            .then(res => {
              if (res.data.errmsg == "成功") {
                this.tableData.splice(this.tableData.length, 1);
                this.tableData.unshift(res.data.data); //实时在列表更新添加
                this.total++; //条数+1
                this.Prosperity.Messages("添加角色成功","success");
                this.dialogFormVisible = false;
              }else{
                _this.Prosperity.Messages("添加角色失败","error");
                 _this.dialogFormVisible = false;
              }
            })
            .catch(err => {
              return err;
            });
        } else {
          return false;
        }
      });
    },
    //点击编辑按钮触发
    handleEdit(index, row) {
      //清空框内
      if (this.$refs.form) {
        this.$refs.form.resetFields();
      }
      this.index = index; //点击时的row下标
      this.form.name = row.name; //让输入框里显示本行姓名
      this.form.desc = row.desc; //让输入框里显示本行说明
      this.form.id = row.id; //获取到本行id并赋值
      this.bool = false; //改变bool 触发编辑接口
      this.title = "编辑"; //改变弹出框标题
      this.dialogFormVisible = true; //显示弹出框
    },
    //编辑角色接口
    compile(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          var _this = this;
          Role.compile(_this.form)
            .then(res => {
              //弹框  如果用户名或者密码等于空的话就弹出
              if (res.data.errmsg == "成功") {
                _this.tableData[_this.index].name = res.data.data.name;
                _this.tableData[_this.index].desc = res.data.data.desc;
                
                _this.Prosperity.Messages("编辑角色成功","success");
                _this.dialogFormVisible = false;
              }else{
                 _this.Prosperity.Messages("编辑角色失败","error");
                 _this.dialogFormVisible = false;
              }
            })
            .catch(err => {
              return err;
            });
          this.dialogFormVisible = false;
          this.cancelbutton();
        } else {
          return false;
        }
      });
    },
    //搜索信息
    seek() {
      this.gain();
    },
    //点击删除当前内容
    handleDelete(index, row) {
      this.cancelIndex = index; //当前删除的下标
      this.id = row.id; //当前删除的id
      this.$confirm(
        "此操作将永久删除该角色---" + row.name + "---, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          //确定删除的话
          this.cancel();
        })
        .catch(() => {
          this.Prosperity.Messages("已取消删除");
        });
    },
    //删除内容接口
    cancel() {
      var _this = this;
      Role.cancel(_this.id)
        .then(res => {
          if (res.data.errmsg == "成功") {
            //如果res返回的值为成功
            _this.gain(); //重新渲染
            _this.Prosperity.Messages("删除角色成功","success");
            _this.dialogFormVisible = false;
          }else{
            _this.Prosperity.Messages("删除角色失败","error");
            _this.dialogFormVisible = false;
          }
        })
        .catch(err => {
          return err;
        });
    },
    //点击授权按钮触发
    popups(index, row) {
      this.id = row.id;
      this.AuthorizaPopup = true;
      this.authorization();
    },
    //授权接口
    authorization() {
      var _this = this;
      Role.authorization(_this.id)
        .then(res => {
          _this.assignedPermissions = res.data.data.assignedPermissions;
          _this.list = res.data.data.systemPermissions; //权限数据渲染
        })
        .catch(err => {
          return err;
        });
    },
    //修改权限确认按钮
    Authoriza() {
      this.AuthorizaPopup = false;
      let permissions = this.$refs.doms.getCheckedKeys(); //使用虚拟dom获取选中数组
      var _this = this;
      Role.Authoriza(_this.id, permissions)
        .then(function(res) {
          if (res.data.errmsg == "成功") {
            _this.Prosperity.Messages("修改角色权限成功","success");
          }else{
            _this.Prosperity.Messages("修改角色权限失败","error");
          }
        })
        .catch(err => {
          return err;
        });
    }
  }
};
</script>

<style scoped="scoped">
/*最大父盒子开始*/
#system_role {
  padding: 20px;
}
/*最大父盒子结束*/

/*顶部index框开始*/
.role_seek > .el-input {
  width: 200px !important;
  height: 40px !important;
}
.role_seek > .el-input > .el-input__inner {
  font-size: inherit;
  width: 50% !important;
  height: 100% !important;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}
.role_input .el-input__suffix {
  left: 170px !important;
}
/*顶部index框结束*/

/*顶部按钮开始*/
.role_seek > .el-button--primary {
  line-height: 1px;
  font-size: 14px;
  border: 1px solid #409eff !important;
  background: #409eff !important;
  color: #fff;
  vertical-align: middle;
  padding: 12px 20px;
  margin-left: 5px;
}
/*顶部按钮结束*/

/*底部列表内容*/
.el-table {
  margin-top: 23px;
}
.cell {
  text-align: center;
  font-size: 12px;
  padding: 0px 10px;
}
.el-table__row > td {
  padding: 8px 0px;
}
.button_redact {
  background: #409eff !important;
  color: #fff !important;
  border: 1px solid #409eff;
} /*编辑内容按钮*/
.button_redact:hover {
  background: #66b1ff !important;
  border: 1px solid #66b1ff;
}
.button_remove {
  background: #f56c6c !important;
  color: #fff !important;
  border: #f56c6c 1px solid;
}
.button_remove:hover {
  background: #f78989 !important;
  border: 1px solid #f78989;
}
.el-table--scrollable-x .el-table__body-wrapper {
  overflow-x: hidden;
}
/*底部列表内容*/

/*底部分页*/
.block {
  margin: 55px 0px;
}
.pagination .el-input__suffix {
  left: 70px !important;
}
/*底部分页结束*/

/*添加弹出框*/
.el-form-item__label {
  text-align: left;
  margin-left: 50px;
  width: 85px !important;
  /* position: relative; */
}
.el-input__inner {
  text-indent: 5px;
}
.pop-up
  > .el-form-item
  > .el-form-item__content
  > .el-input
  > .el-input__inner {
  width: 268px !important;
  height: 38px;
  padding: 15px 0px;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
}
.pop-up
  > .el-form-item
  > .el-form-item__content
  > .el-input
  > .el-input__suffix {
  left: 198px !important;
  right: 0px !important;
}
.pop-up > .el-form-item > .el-form-item__content > .el-form-item__error {
  left: 0px !important;
}
/*添加弹出框结束*/

/*权限弹出框开始*/
.dialog-footer {
  text-align: right !important;
}
.title {
  text-align: left !important;
  /* overflow: hidden; */
}
.title .el-tree-node__content > .el-checkbox {
  width: 2% !important;
  margin-bottom: 0px !important;
  margin-top: 0px !important;
}
.title > .el-dialog {
  box-shadow: none !important;
}
/*权限弹出框结束*/
</style>
